<?php

namespace app\api\controller\v2;

use app\api\controller\Common;
use think\Db;
use think\Request;

class Group extends Common {
    /**
     * @var mixed
     */
    public $params = null;
    /**
     * @param Request $request
     */
    public function __construct(Request $request = null) {
        parent::__construct($request);
        $this->params = getRsaPostParamArr2();
    }

    public function list() {
        // $this->params['page']     = 1;
        // $this->params['pagesize'] = 10;
        // $this->params['join'] = 0;

        $where = ['state' => 1];
        if ($this->params['join']) {
            $joined = Db::name('share_group_join')->where(['user_id'=>$this->member['id'], 'state'=>1])->column('group_id');
            $where['id'] = ['in',$joined];
        }
        $data  = Db::name('share_group')
            ->where($where)
            ->page($this->params['page'])
            ->limit($this->params['pagesize'])
            ->field('create_time,update_time,audit_time,create_uid', 'filter')
            ->select();

        $joined = Db::name('share_group_join')->where(['user_id'=>$this->member['id'], 'group_id'=>['in',array_column($data, 'id')], 'state'=>1])->column('group_id');

        foreach ($data as &$value) {
            $value = $this->formatData($value);

            $value['joined'] = in_array($value['id'], $joined);
        }

        return $this->pass($data);
    }

    /**
     * @return mixed
     */
    public function info($returnJson = true) {
        // $this->params['id'] = 1;

        $data = Db::name('share_group')->where(['state' => 1, 'id' => $this->params['id']])->field('create_time,update_time,audit_time,create_uid', 'filter')->find();

        $joined = Db::name('share_group_join')->where(['user_id'=>$this->member['id'], 'group_id'=>$data['id'], 'state'=>1])->column('group_id');
        if ($returnJson) {
            $data = $this->formatData($data);
        }
        $data['joined'] = in_array($data['id'], $joined);

        return $returnJson ? $this->pass($data) : $data;
    }

    /**
     * @param array $value
     * @return mixed
     */
    private function formatData($value = []) {
        $tags = [];
        if ($value['agemin'] || $value['agemax']) {
            if ($value['agemin'] && $value['agemax']) {
                $tags[] = $value['agemin'] . '~' . $value['agemax'] . '岁';
            } elseif ($value['agemin']) {
                $tags[] = $value['agemin'] . '岁以上';
            } elseif ($value['agemax']) {
                $tags[] = $value['agemax'] . '岁以下';
            }
        }

        if ($value['sex']) {
            $tags[] = $value['sex'] . '性';
        }
        if ($value['realface']) {
            $tags[] = '真人';
        }
        if ($value['realname']) {
            $tags[] = '实名';
        }
        if ($value['is_education_authentication']) {
            $tags[] = $value['is_education_authentication'] . '及以上';
        }
        if ($value['car_auth']) {
            $tags[] = '已购车';
        }
        if ($value['is_house_purchase']) {
            $tags[] = '已购房';
        }
        if ($value['income_auth']) {
            $tags[] = '收入认证';
        }
        if ($value['marry_auth']) {
            $tags[] = '婚姻状况认证';
        }

        $value['requires'] = $tags;
        unset($value['agemin'], $value['agemax'], $value['sex'], $value['realface'], $value['realname'], $value['is_education_authentication'], $value['car_auth'], $value['is_house_purchase'], $value['income_auth'], $value['marry_auth']);
        return $value;
    }

    public function join()
    {
        if ($this->member['noble_grade'] < 1 || $this->member['noble_time'] < time()) {
            return $this->miss('仅对VIP开放', 201);
        }
        $info = $this->info(false);
        if ($info['joined']) {
            return $this->miss('你已加入');
        }
        // 条件审核
        if ($info['agemin'] || $info['agemax']) {
            if ($info['agemin'] && $info['agemax']) {
                if (!($this->member['age'] >= $info['agemin'] && $this->member['age'] <= $info['agemax'])) {
                    return $this->miss('进组年龄要求' . $info['agemin'] . '~' . $info['agemax'] . '岁');
                }
            } elseif ($info['agemin']) {
                if (!($this->member['age'] >= $info['agemin'])) {
                    return $this->miss('进组年龄要求' . $info['agemin'] . '岁以上');
                }
            } elseif ($info['agemax']) {
                if (!($this->member['age'] <= $info['agemax'])) {
                    return $this->miss('进组年龄要求' . $info['agemax'] . '岁以下');
                }
            }
        }

        if ($info['sex']) {
            if (!($this->member['sex'] == $info['sex'])) {
                return $this->miss('进组性别要求' . $info['sex'] . '性');
            }
        }
        if ($info['realface']) {
            if (!($this->member['is_identity_authentication'] == 2)) {
                return $this->miss('进组要求完成真人认证');
            }
        }
        if ($info['realname']) {
            if (!($this->member['realname_auth'] == 2)) {
                return $this->miss('进组要求完成实名认证');
            }
        }
        if ($info['is_education_authentication']) {
            $edus = ['初中','中专','高中','大专','本科','双学士','硕士','博士'];
            if (!(array_search($this->member['education_background'], $edus) >= array_search($info['is_education_authentication'], $edus))) {
                return $this->miss('进组学历要求'.$info['is_education_authentication'].'及以上');
            }
        }
        if ($info['car_auth']) {
            if (!($this->member['car_auth'] == 2)) {
                return $this->miss('进组要求完成购车认证');
            }
        }
        if ($info['is_house_purchase']) {
            if (!($this->member['is_house_purchase'] == 2)) {
                return $this->miss('进组要求完成购房认证');
            }
        }
        if ($info['income_auth']) {
            if (!($this->member['income_auth'] == 2)) {
                return $this->miss('进组要求完成收入认证');
            }
        }
        if ($info['marry_auth']) {
            if (!($this->member['marry_auth'] == 2)) {
                return $this->miss('进组要求完成情感状态认证');
            }
        }

        $join = Db::name('share_group_join')->insert([
            'user_id'   =>  $this->member['id'],
            'group_id'  => $info['id'],
            'join_time' => time(),
            'state'     => 1
        ]);
        if ($join) {
            Db::name('share_group')->where(['id'=>$info['id']])->setInc('count');
            return $this->pass('加入成功');
        }
        return $this->miss('请重试');
    }

    public function exit()
    {
        $info = $this->info(false);
        if (!$info['joined']) {
            return $this->miss('您未加入该小组');
        }
        $exit = Db::name('share_group_join')->where(['user_id'=>$this->member['id'], 'group_id'=>$info['id'], 'state'=>1])->update(['state'=>-2, 'exit_time'=>time()]);
        if ($exit) {
            Db::name('share_group')->where(['id'=>$info['id']])->setDec('count');
            return $this->pass('退出成功');
        }
        return $this->miss('请重试');
    }

    public function member()
    {
        // $this->params['id'] = 1;
        // $this->params['page'] = 1;
        // $this->params['pagesize'] = 1;
        $where = ['a.state'=>1,'u.reg'=>1,'u.delete_time'=>0];
        $where['a.group_id'] = $this->params['id'];
        $field = 'a.id as sid, u.id user_id,u. name user_name,u. head_100 user_head,u. sex user_sex,u. age user_age,u. city,u.
                img_auth,u. is_online,u. is_identity_authentication,u. last_login_datetime,u. nvshen,u. hide_online_time,u.
                noble_grade,u. noble_time,u. hide_distance,u. kejian,u. xuni_discount,u. notlimit_city,u. role,u. voice,u.
                voice_time,u. voice_auth,u. realname_auth,u. register_datetime,u. yqr,u. real_avatar,u. occupation,u. auth_tag,u. entertainment,u.self_slogan, u.monthly_salary, u.income_auth, u.education_background, u.is_education_authentication, u.is_house_purchase, u.house_auth, u.is_car_purchase, u.car_auth, u.emotion, u.marry_auth';
        $data = Db::name('share_group_join')
                    ->alias('a')
                    ->join('user u','a.user_id=u.id')
                    ->where($where)
                    ->field($field)
                    ->page($this->params['page'])
                    ->limit($this->params['pagesize'])
                    ->select();
        return $this->pass($data);
    }
}